<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row class="ml10" v-show="showSearch">
				<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef">
					<el-form-item label="设备" prop="equipmentCode">
						<el-select v-model="state.queryForm.equipmentCode" placeholder="请选择设备">
							<el-option v-for="item in equipmentList" :key="item.id" :label="item.name" :value="item.equipmentCode" />
						</el-select>
					</el-form-item>
					<el-form-item :label="$t('syslog.createTime')" prop="dateTime">
						<el-date-picker
							:end-placeholder="$t('syslog.inputEndPlaceholderTip')"
							:start-placeholder="$t('syslog.inputStartPlaceholderTip')"
							range-separator="To"
							type="datetimerange"
							v-model="state.queryForm.dateTime"
							value-format="YYYY-MM-DD HH:mm:ss"
						/>
					</el-form-item>
					<el-form-item>
						<el-button @click="getDataList" icon="Search" type="primary">{{ $t('common.queryBtn') }} </el-button>
						<el-button @click="resetQuery" icon="Refresh">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="flex justify-between mb8" style="width: 100%">
					<div class="flex">
						<el-button plain :disabled="multiple" icon="Delete" type="danger" v-auth="'platform_deviceLog_del'" @click="handleDelete(selectObjs)">
							删除
						</el-button>
						<span class="flex items-center ml-2 align-baseline text-error">
							<el-icon size="14"><WarningFilled /></el-icon>
							<span class="text-error">当前版本只支持查询{{ dataPeriod }}天的数据。</span>
						</span>
					</div>

					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'platform_deviceLog_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				@selection-change="handleSelectionChange"
				@sort-change="sortChangeHandle"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column prop="equipmentCode" label="设备编码" width="150" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="createTime" label="创建时间" width="180" sortable="custom" show-overflow-tooltip />
				<el-table-column prop="content" label="报文" show-overflow-tooltip />
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button icon="view" @click="LogDetailRef.openDialog(scope.row)" size="small" text type="primary">
							{{ $t('common.detailBtn') }}
						</el-button>
						<el-button icon="delete" text type="danger" v-auth="'platform_deviceLog_del'" @click="handleDelete([scope.row.id])">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>
		<log-detail ref="LogDetailRef"></log-detail>
	</div>
</template>

<script setup lang="ts" name="systemDeviceLog">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/platform/deviceLog';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useI18n } from 'vue-i18n';
import { fetchListAll } from '/@/api/platform/airMonitorEquipment';
import { getObj } from '/@/api/admin/tenant';
import { Session } from '/@/utils/storage';
const LogDetail = defineAsyncComponent(() => import('./detail.vue'));
// 定义查询字典

const { t } = useI18n();
// 搜索变量
const queryRef = ref();
const LogDetailRef = ref();
const showSearch = ref(true);
const dataPeriod = ref(0);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);
const equipmentList: any = ref([]);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	createdIsNeed: false,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/platform/deviceLog/export', state.queryForm, 'deviceLog.xlsx');
};

// 多选事件
const handleSelectionChange = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 获取租户套餐信息
const getTenantInfo = () => {
	const id = Session.getTenant();
	getObj(id).then((res) => {
		dataPeriod.value = res.data.dataPeriod;
	});
};
getTenantInfo();
// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
// 获取所有设备列表
const getEquipmentList = () => {
	fetchListAll().then((res) => {
		equipmentList.value = res.data || [];
	});
};
getEquipmentList();
</script>
